﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok
{
	[TestFixture]
	public class Test_FailureException
	{
		[Test]
		public void Test_Construction_FailureNumber()
		{
			var exception = new FailureException(0x12345678);

			Assert.That(exception.Arguments, Is.Empty);
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.Null);
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: )"));
		}

		[Test]
		public void Test_Construction_FailureNumber_Arguments()
		{
			var exception = new FailureException(0x12345678, "ABC", 123);

			Assert.That(exception.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception.Arguments[1], Is.EqualTo(123));
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.Null);
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}

		[Test]
		public void Test_Construction_FailureNumber_Exception()
		{
			var innerException = new ApplicationException();
			var exception = new FailureException(0x12345678, innerException);

			Assert.That(exception.Arguments, Is.Empty);
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.SameAs(innerException));
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: )"));
		}

		[Test]
		public void Test_Construction_FailureNumber_Exception_Arguments()
		{
			var innerException = new ApplicationException();
			var exception = new FailureException(0x12345678, innerException, "ABC", 123);

			Assert.That(exception.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception.Arguments[1], Is.EqualTo(123));
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.SameAs(innerException));
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}


		[Test]
		public void Test_Construction_FacilityCode_FailureCode()
		{
			var exception = new FailureException(0x1234, 0x5678);

			Assert.That(exception.Arguments, Is.Empty);
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.Null);
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: )"));
		}

		[Test]
		public void Test_Construction_FacilityCode_FailureCode_Arguments()
		{
			var exception = new FailureException(0x1234, 0x5678, "ABC", 123);

			Assert.That(exception.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception.Arguments[1], Is.EqualTo(123));
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.Null);
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}

		[Test]
		public void Test_Construction_FacilityCode_FailureCode_Exception()
		{
			var innerException = new ApplicationException();
			var exception = new FailureException(0x1234, 0x5678, innerException);

			Assert.That(exception.Arguments, Is.Empty);
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.SameAs(innerException));
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: )"));
		}

		[Test]
		public void Test_Construction_FacilityCode_FailureCode_Exception_Arguments()
		{
			var innerException = new ApplicationException();
			var exception = new FailureException(0x1234, 0x5678, innerException, "ABC", 123);

			Assert.That(exception.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception.Arguments[1], Is.EqualTo(123));
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.SameAs(innerException));
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}


		[Test]
		public void Test_Construction_FailureException()
		{
			var innerException = new ApplicationException();
			var failureException = new FailureException(0x1234, 0x5678, innerException, "ABC", 123);
			var exception = new FailureException(failureException);

			Assert.That(exception.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception.Arguments[1], Is.EqualTo(123));
			Assert.That(exception.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception.InnerException, Is.SameAs(failureException));
			Assert.That(exception.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}


		[Test]
		public void Test_Serialization_BinaryFormatter()
		{
			var innerException = new ApplicationException();
			var failureException = new FailureException(0x1234, 0x5678, innerException, "ABC", 123);
			var exception1 = new FailureException(failureException);

			var exception2 = TestHelper.TestBinaryFormatterRoundtrip<FailureException>(exception1);

			Assert.That(exception2, Is.Not.SameAs(exception1));
			Assert.That(exception2.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception2.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception2.Arguments[1], Is.EqualTo(123));
			Assert.That(exception2.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception2.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception2.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception2.InnerException, Is.Not.SameAs(failureException));
			Assert.That(exception2.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception2.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception2.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}

		[Test]
		public void Test_Serialization_DataContract()
		{
			var innerException = new ApplicationException();
			var failureException = new FailureException(0x1234, 0x5678, innerException, "ABC", 123);
			var exception1 = new FailureException(failureException);

			var exception2 = TestHelper.TestDataContractRoundtrip<FailureException>(exception1,
				typeof(ApplicationException),
				typeof(Object[]));

			Assert.That(exception2, Is.Not.SameAs(exception1));
			Assert.That(exception2.Arguments, Has.Length.EqualTo(2));
			Assert.That(exception2.Arguments[0], Is.EqualTo("ABC"));
			Assert.That(exception2.Arguments[1], Is.EqualTo(123));
			Assert.That(exception2.FailureNumber, Is.EqualTo(0x12345678));
			Assert.That(exception2.FacilityCode, Is.EqualTo(0x1234));
			Assert.That(exception2.FailureCode, Is.EqualTo(0x5678));
			Assert.That(exception2.InnerException, Is.Not.SameAs(failureException));
			Assert.That(exception2.Message, Is.StringContaining("0x12345678"));
			Assert.That(exception2.ToString(), Is.StringContaining("0x12345678"));
			Assert.That(exception2.ToString(), Is.StringContaining("Arguments: ABC;123)"));
		}

	}
}

#endif
